using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Daenet.Syndication
{
    public class SyndicationPerson
    {
        #region Member Fields

        private string m_Name;

        private string m_Email;

        private string m_Uri;

        #endregion

        #region Member Properties

        /// <summary>
        /// Name of the Person
        /// </summary>
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }

        /// <summary>
        /// Email of the Person
        /// </summary>
        public string Email
        {
            get { return m_Email; }
            set { m_Email = value; }
        }

        /// <summary>
        /// Url to person's homepage
        /// </summary>
        public string Uri
        {
            get { return m_Uri; }
            set { m_Uri = value; }
        }

        #endregion

        #region Member Methods

        /// <summary>
        /// Public Constructor (default)
        /// </summary>
        public SyndicationPerson()
        {
        }

        /// <summary>
        /// Public constructor
        /// </summary>
        /// <param name="name"></param>
        /// <param name="email"></param>
        /// <param name="uri"></param>
        public SyndicationPerson(string name, string email, string uri)
        {
            this.m_Email = email;
            this.m_Name = name;
            this.m_Uri = uri;
        }

        /// <summary>
        /// Static Method that parses the Percon Construct
        /// This construct can be either author or contributor
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static SyndicationPerson TryParse(XmlReader reader)
        {
            SyndicationPerson person = new SyndicationPerson();

            while (true)
            {
                reader.Read();

                if (reader.NodeType == XmlNodeType.Whitespace)
                {
                    reader.Read();
                }
                if (reader.Name == "name" && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    person.Name = reader.Value;
                }
                else if (reader.Name == "uri" && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    person.Uri = reader.Value;
                }
                else if (reader.Name == "email" && reader.NodeType == XmlNodeType.Element)
                {
                    reader.Read();
                    person.Email = reader.Value;
                }
                else if (reader.Name == "author" && reader.NodeType == XmlNodeType.EndElement)
                    break;
                else if (reader.Name == "contributor" && reader.NodeType == XmlNodeType.EndElement)
                    break;
            }

            return person;
        }

        #endregion
    }
}
